package cn.sjx.auth.service.impl;

import cn.sjx.auth.annotation.Permission;
import cn.sjx.auth.domain.Permission1;
import cn.sjx.auth.mapper.PermissionMapper;
import cn.sjx.auth.query.PermissionQuery;
import cn.sjx.auth.service.PermissionService;
import cn.sjx.basic.service.BasicService;
import cn.sjx.basic.service.impl.BasicServiceImpl;
import cn.sjx.basic.utils.BasicQuery;
import cn.sjx.basic.utils.ClassUtils;
import cn.sjx.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

@Service
public class PermissionServiceImpl extends BasicServiceImpl<Permission1> implements PermissionService {


    @Value("${ronghua.permission.scan-base-package}")
    private String scanBasePackage;


    @Autowired
    private PermissionMapper permissionMapper;



    /**
     * 权限扫描
     */
    @Override
    public void scan() {
        //1 拿到所有的controller，因为只有controller才有注解
        List<Class> classes = ClassUtils.getAllClassName(scanBasePackage);
        System.out.println(classes);

        //先删除权限，然后再添加
        permissionMapper.deleteAll();


        for (Class aClass : classes) {
            //2 判断类上是否有@Permission
            Permission classAnnotation = (Permission) aClass.getAnnotation(Permission.class);
            if (Objects.isNull(classAnnotation)) {
                //3 如果为空，跳过 continue
                continue;
            }

            RequestMapping requestMapping = (RequestMapping) aClass.getAnnotation(RequestMapping.class);
            String classUrl = requestMapping.value()[0];
            //类上的注解也要解析成一个Permission对象，它是方法权限的父权限，要通过它来获得parentId
            Permission1 parent = new Permission1();
            parent.setName(classAnnotation.name());
            parent.setDescs(classAnnotation.descs());
            parent.setUrl(classUrl);
            parent.setSn(aClass.getSimpleName());
            //保存父权限
            permissionMapper.insert(parent);


            //4 如果有，拿到类里面所有方法
            Method[] methods = aClass.getMethods();
            //5 循环所有方法，判断方法上是否有@Permission
            for (Method method : methods) {
                Permission methodAnnotation = method.getAnnotation(Permission.class);
                if (Objects.isNull(methodAnnotation)) {
                    //6 如果方法上没有，跳过
                    continue;
                }
                //7 如果方法上有，则解析@Permission，封装Permission对象
                Permission1 permission = new Permission1();
                permission.setName(methodAnnotation.name());
                permission.setDescs(methodAnnotation.descs());
                //处理url 类上的url+方法上的url,此处用的工具类
                permission.setUrl(ClassUtils.getUrl(aClass, method));
                //处理sn唯一标识
                permission.setSn(aClass.getSimpleName() + ":" + method.getName());
                //处理父权限Id
                permission.setParentId(parent.getId());


                //8 保存Permission对象
                permissionMapper.insert(permission);
            }
        }

    }

    @Override
    public PageList<Permission1> pageList(BasicQuery basicQuery) {

         Long total = permissionMapper.loadTotal(basicQuery);
        if (total == 0) {
            return new PageList<>();
        }else {
            List<Permission1> list = permissionMapper.loadData(basicQuery);
            return new PageList<>(total,list);
        }

    }



    @Override
    public List<String> getPermissionSns() {
        return permissionMapper.getPermissionSns();
    }
}
